//
//  ArticleTableView.m
//  one·一个  tyw
//
//  Created by tyw on 15/9/7.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "ArticleTableView.h"
#import "DayNightLabel.h"
#import "DayNightImgView.h"
#import "Tools.h"
#import "ArticleTableViewCell.h"

@implementation ArticleTableView {
    DayNightLabel* likeCountLabel;
}

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        self.backgroundColor = [UIColor clearColor];
        
        self.delegate = self;
        self.dataSource = self;
        
        //注册
        [self registerClass:[ArticleTableViewCell class] forCellReuseIdentifier:kArticleTableViewCellId];
    }
    
    return self;
}

#pragma mark - setDataDic
- (void)setDataDic:(NSDictionary *)dataDic {
    _dataDic = dataDic;
    
    [self reloadTableHeadFootView];         //刷新表视图的头部和尾部视图
    [self reloadData];                      //刷新表视图
}

#pragma mark - reloadTableHeadFootView
- (void)reloadTableHeadFootView {
    //时间、文章标题、作者名，作为头部视图
    UIView* headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 50)];
    self.tableHeaderView = headView;
    //时间
    DayNightLabel* timeLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 0, kWidth-20, 20)];
    [timeLabel loadLabelColor];
    timeLabel.text = [Tools exchangeDate:_dataDic[@"strContMarketTime"]];
    timeLabel.textAlignment = NSTextAlignmentLeft;
    timeLabel.font = [UIFont systemFontOfSize:10];
    [headView addSubview:timeLabel];
    //标题
    DayNightLabel* titleLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 20, kWidth-20, 20)];
    [titleLabel loadLabelColor];
    titleLabel.text = _dataDic[@"strContTitle"];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = [UIFont systemFontOfSize:15];
    [headView addSubview:titleLabel];
    //作者名
    DayNightLabel* authorLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 40, kWidth-20, 10)];
    [authorLabel loadLabelColor];
    authorLabel.text = _dataDic[@"strContAuthor"];
    authorLabel.textAlignment = NSTextAlignmentLeft;
    authorLabel.font = [UIFont systemFontOfSize:8];
    [headView addSubview:authorLabel];
    
    
    //责任编辑、赞、作者、作者简介  作为尾部视图
    UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 150)];
    self.tableFooterView = footerView;
    //责任编辑
    DayNightLabel* contAuthorIntroduceLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 0, kWidth-20, 20)];
    [contAuthorIntroduceLabel loadLabelColor];
    contAuthorIntroduceLabel.text = _dataDic[@"strContAuthorIntroduce"];
    contAuthorIntroduceLabel.textAlignment = NSTextAlignmentLeft;
    contAuthorIntroduceLabel.font = [UIFont systemFontOfSize:10];
    [footerView addSubview:contAuthorIntroduceLabel];
    //赞 背景
    UIImageView* likeBgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth-80, 30, 80, 30)];
    UIImage* image = [UIImage imageNamed:@"home_likeBg"];
    image = [image stretchableImageWithLeftCapWidth:40 topCapHeight:0];     //拉伸
    likeBgImgView.contentMode = UIViewContentModeScaleAspectFit;
    likeBgImgView.image = image;
    //likeBgImgView.backgroundColor = [UIColor redColor];
    likeBgImgView.userInteractionEnabled = YES;
    [footerView addSubview:likeBgImgView];
    //赞 心
    UIButton* likeBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 8, 15, 15)];
    [likeBtn setImage:[UIImage imageNamed:@"home_like"] forState:UIControlStateNormal];
    [likeBtn setImage:[UIImage imageNamed:@"home_like_hl"] forState:UIControlStateSelected];
    [likeBtn addTarget:self action:@selector(likeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [likeBgImgView addSubview:likeBtn];
    //赞数量
    likeCountLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(30, 8, likeBgImgView.bounds.size.width-20, 15)];
    NSString* likeCount = _dataDic[@"strPraiseNumber"];
    likeCountLabel.text = likeCount;
    likeCountLabel.textAlignment = NSTextAlignmentCenter;
    likeCountLabel.font = [UIFont systemFontOfSize:8];
    [likeCountLabel loadLabelColor];
    [likeBgImgView addSubview:likeCountLabel];
    //分割线
    DayNightImgView* line = [[DayNightImgView alloc] initWithFrame:CGRectMake(10, 74.5, kWidth-20, 1)];
    [line setImgName:@"colLine"];
    [footerView addSubview:line];
    //作者
    DayNightLabel* authorFooterLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 90, 50, 20)];
    [authorFooterLabel loadLabelColor];
    authorFooterLabel.text = _dataDic[@"strContAuthor"];
    authorFooterLabel.textAlignment = NSTextAlignmentLeft;
    authorFooterLabel.font = [UIFont systemFontOfSize:16];
    [footerView addSubview:authorFooterLabel];
    //作者微博
    DayNightLabel* sWbNameLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(60, 95, kWidth-70, 15)];
    [sWbNameLabel loadLabelColor];
    sWbNameLabel.text = _dataDic[@"sWbN"];
    sWbNameLabel.textAlignment = NSTextAlignmentLeft;
    sWbNameLabel.font = [UIFont systemFontOfSize:10];
    [footerView addSubview:sWbNameLabel];
    //作者简介
    DayNightLabel* sAuthorLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 110, kWidth-20, 40)];
    [sAuthorLabel loadLabelColor];
    sAuthorLabel.text = _dataDic[@"sAuth"];
    sAuthorLabel.textAlignment = NSTextAlignmentLeft;
    sAuthorLabel.font = [UIFont systemFontOfSize:12];
    sAuthorLabel.numberOfLines = 0;
    [footerView addSubview:sAuthorLabel];
}

#pragma mark - tableViewDelegateDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString* content = _dataDic[@"strContent"];        //正文
    NSArray* array = [content componentsSeparatedByString:@"<br><br>"];     //以此为分割线，分割成各个大段落，每个段落是一个cell
    
    return array.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ArticleTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kArticleTableViewCellId forIndexPath:indexPath];
    
    NSString* content = _dataDic[@"strContent"];        //正文
    NSArray* array = [content componentsSeparatedByString:@"<br><br>"];     //以此为分割线，分割成各个大段落，每个段落是一个cell
    NSString* str = array[indexPath.row];
    
    cell.contentStr = str;
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* content = _dataDic[@"strContent"];
    NSArray* array = [content componentsSeparatedByString:@"<br><br>"];
    if (array.count > 0) {
        NSString* str = array[indexPath.row];
        NSString* newStr = [str stringByReplacingOccurrencesOfString:@"<br>" withString:@"    \n    "];
        CGFloat height = [Tools contentSizeWithMaxWidth:kWidth-20 text:newStr fontOfSize:14].height;
        
        return height+20;
    }
    
    return 80;
}


#pragma mark - btnAction
- (void)likeBtnAction:(UIButton*)button {
    button.selected = !button.selected;
    
    NSInteger likeCount = [likeCountLabel.text integerValue];
    likeCount = button.selected?likeCount+1:likeCount-1;
    likeCountLabel.text = [NSString stringWithFormat:@"%li", (long)likeCount];
}

@end
